# -*- coding: utf-8 -*-
import json
import csv
import re
import operator
import sys
import importlib
importlib.reload(sys)
import os
import io
from config import *



'''涉案人及涉案人数判定,传参为一个完整文本'''
def person_extract(str1):
	text=str1
	person=[]
	person_num=0
	content = text.split("。")
	#extract person
	for i in range(len(content)):
		for j in name_prefix:
			if(re.search(j,content[i])!=None):
				content2=content[i].split("，")
				tt=""
				if(len(content2)>0):
					tt=content2[0]
				tt2=tt.split(j)
				if(len(tt2)>1):
					tt3=tt2[len(tt2)-1]
					if(len(tt3)<4):
						name=tt3					
						if (len(name)> 0 and name not in person and name[0] in surnames):
							person.append(name)
					else:
						tt4=tt3.split("）")
						if(len(tt4)>1 and len(tt4[len(tt4)-1])<4):
							name=tt4[len(tt4)-1]
							if (len(name)> 0 and name not in person and name[0] in surnames):
								person.append(name)
				if(len(person)==0):
					for k in name_prefix2:
						tt2=tt.split(k)
						if(len(tt2)>1):
							tt3=tt2[1].split("）")
							if(len(tt3)>1):
								if(len(tt3[1])<4):
									name=tt3[1]
									if (len(name)> 0 and  name not in person and name[0] in surnames):
										person.append(name)
								else:
									tt4=tt3[1].split("（")
									if(len(tt4[0])<4):
										name=tt4[0]
										if (len(name)> 0 and name not in person and name[0] in surnames):
											person.append(name)
							elif(len(tt3)==1 and len(tt3[0])<4):
								name=tt3[0]
								if (len(name)> 0 and name not in person and name[0] in surnames):
									person.append(name)
					if(len(person)==0):
						for k in name_prefix3:
							tt2=tt.split(k)
							if(len(tt2)>1):
								tt3=tt2[len(tt2)-1]
								if(len(tt3)<4):
									name=tt3
									if (len(name)> 0 and name not in person and name[0] in surnames):
										person.append(name)
								else:
									tt4=tt3.split("（")
									if(len(tt4)>1 and len(tt4[0])<4):
										name=tt4[0]
										if (len(name)> 0 and name not in person and name[0] in surnames):
											person.append(name)	
		if(len(person)==0):
			the_flag=0
			for k in name_supplement:
				if(re.search(k,content[i])!=None):
					the_flag+=1
			if(the_flag==len(name_supplement)):
				kk=content[i]
				a=kk.split(name_prefix3[0])
				if(len(a)>1):
					b=a[1].split(name_supplement2)
					if(len(b)>1):
						name=b[0]
						if (len(name)> 0 and name not in person and name[0] in surnames):
							person.append(name)																																												
	person_num=str(len(person))												
	return person,person_num

'''提取金额,传参为一个完整文本及抽取出来的判决人'''
def fine(str,person):
	fine_txt={}#罚金
	moshou_all_txt={}#没收全部财产
	moshou_some_txt={}#没收部分财产
	tuipei_txt={}#责令退赔
	zangkuan_txt={}#赃款赃物
	own_txt={}#供犯人所用本人财务
	other_txt={}#其他
	
	oldmoney_txt={}#原文本金额
	money=""
	old_money=""
	for i in money_prefix1:
		if re.search(i,str) != None:
			txt = str.split(i) 
			txt2=txt[len(txt)-1]
			texts=txt2.split('。')
			for lines in texts:
				for k in money_prefix3:#罚金
					if re.search(k, lines):
						for people in person:
							if re.search(people, lines):
								a=lines.split(k)
								c=a[1].split('；')
								for suf in money_suffix1:
									if re.search(suf, c[0]):
										d=c[0].split(suf)
										money=d[0]
										for pre in money_prefix2:
											if(re.search(pre,money)!=None):
												money2=money.split(pre)
												if(len(money2)>1):
													money3=money2[1]
													old_money=pre+money3+suf
													oldmoney_txt[people]=old_money	
													money4=money2int(money3)
													fine_txt[people]=money4
													
				for k in money_prefix4:#没收财产
					if re.search(k, lines):
						for people in person:
							if re.search(people, lines):
								a=lines.split(k)
								c=a[1].split('；')
								for suf in money_suffix1:
									if re.search(suf, c[0]):
										d=c[0].split(suf)
										money=d[0]
										for pre in money_prefix2:
											if(re.search(pre,money)!=None):
												money2=money.split(pre)
												if(len(money2)>1):
													money3=money2[1]
													old_money=pre+money3+suf
													oldmoney_txt[people]=k+old_money
													money4=money2int(money3)
													if re.search(money_prefix5, lines):#没收全部财产
														moshou_all_txt[people]=money4
													else:#没收部分财产
														moshou_some_txt[people]=money4
				if re.search(money_prefix6, lines):	#赃款赃物
					for people in person:
						if re.search(people, lines):
							a=lines.split(money_prefix6)
							c=a[1].split('；')
							for suf in money_suffix1:
								if re.search(suf, c[0]):
									d=c[0].split(suf)
									money=d[0]
									for pre in money_prefix2:
										if(re.search(pre,money)!=None):
											money2=money.split(pre)
											if(len(money2)>1):
												money3=money2[1]
												old_money=pre+money3+suf
												oldmoney_txt[people]=old_money	
												money4=money2int(money3)
												zangkuan_txt[people]=money4														
																																					
	return 	oldmoney_txt,fine_txt,moshou_all_txt,moshou_some_txt,tuipei_txt,zangkuan_txt,own_txt,other_txt																								

'''金额转换为数字,传参为一个str'''
def money2int(str): 		
	common_used_numerals = {}
	order = {}
	for key in common_used_numerals_tmp:
		common_used_numerals[key] = common_used_numerals_tmp[key]
		order[key] = order_tmp[key]	

	money=0
	flag=0
	if(re.search(",",str)!=None):
		k=""
		a=str.split(",")
		for i in a:
			k+=i
		money=int(k)
	elif(re.search("，",str)!=None):
		k=""
		a=str.split("，")
		for i in a:
			k+=i
		money=int(k)						
	elif(str.isdigit()==True):
		money=int(str)
	elif(re.search("\.",str)!=None):
		for key,value in money_conversion2.items():
			if(re.search(key,str)!=None):
				a=str.split(key)
				b=float(a[0])
				c=b*int(value)
				money=int(c)
				flag=1				
		if(flag==0):
			a=str.split("\.")
			if(a[0].isdigit()==True):
				k=a[0]
				money=int(k)
	elif(re.search("�",str)!=None):
		print ("hehe")
	else:
		a=str
		for key,value in money_conversion.items():
			if(re.search(key,a)!=None):
				a=a.replace(key,value)
		if(a!=None):
			money=comput(a, common_used_numerals, order)
	return money						

'''中文转换为数字,迭代算法'''
def comput(cn_digits, common_used_numerals, order):	
    # get the length of chinese digits
    i = len(cn_digits) - 1
    # if the length is zero, we return 1
    if len(cn_digits) == 0:
        return 1
    # if the length is 1 ,or length is 2 and the first digit is zero,we return the second digit directly
    elif len(cn_digits) == 1 or (len(cn_digits) == 2 and common_used_numerals[cn_digits[i-1]] == 0 and common_used_numerals[cn_digits[i]] > 0):
        return common_used_numerals.get(cn_digits[i])
    # if the length >=2 and the first is not zero,then divide it into two parts
    else:
        # search the i, that i-indexed digit 's order is high than the last digit
        while (i-1) >= 0 and order.get(cn_digits[i-1]) <= order.get(cn_digits[-1]):
            i -= 1
        # if all the digits'order before the last is lower than the last's(eg. 三百四十五万)，divide it into one part
        if i == 0:
            return int(comput(cn_digits[0:len(cn_digits) - 1], common_used_numerals, order)) * int(order.get(cn_digits[i - 1]))
        # if it's ordinary(eg. 三百四十万六千二百零一) ,we divide it into two parts
        else:
            return int(comput(cn_digits[0:i], common_used_numerals, order)) + int(comput(cn_digits[i:len(cn_digits)], common_used_numerals, order)) * 1

#前缀为“本院认为”“本院再审认为，”“本院认为：”“本院再审认为：”等词汇
#且文本中存在“因”，“构成”等词汇。抽取规则：文本中第一个满足上述多个条件的句子。 
'''罪行判定,传参为一个完整文本'''
def crimes_determine(str):
    text=str
    crime_txt=''
    content = text.split("。")
    for i in range(len(content)):
        for j in range(len(crimes_determine_key)):
            if(re.search(crimes_determine_key[j],content[i])!=None):
                tt=content[i].split(crimes_determine_key[j])
                if(len(tt)>1):
                    crime_txt=tt[1]+"。"
    return crime_txt

'''罪名抽取和案件类型抽取,传参为一个完整文本和人名
抽取规则：罪名一般出现在判决如下里，形如“被告人+姓名+犯+罪名+罪”
罪名和人名需要一一对应，因此根据之前提取的被告人姓名，去判决如下里抽取。
案件类型取第一个人的第一个罪名'''
def cname(str,person):
	charge_name = []
	charge_name_txt={}
	for result in result_key:
		if re.search(result,str) != None: 
		#从“判决如下”之后开始判断
			test1 = str.split(result)
			#用“判决如下进行分割”
			a = len(test1) - 1
			test1 = re.split('。|;|；',test1[a])
			#将“判决如下”分割的最后一个内容部分在按行分割
			for i in person:
				for j in range(0,len(test1)):
					test2 = re.split(i,test1[j])
					for k in range(1,len(test2)):
						for key in cname_key1:
							if re.search(key,test2[k]) != None:
								test3 = re.split(key,test2[k])
								test4 = re.split(cname_key2,test3[1])
								if test4[0] == '':
									continue
								else:
									if i in charge_name and test4[0] in charge_name:
										break
									else:
										if i in charge_name and test4[0] not in charge_name:
											charge_name[charge_name.index(i)+1] = test4[0]
											break
										else:
											charge_name.append(i)
											charge_name.append(test4[0]) 
			'''if len(charge_name) == 0:
				for i in cname_key1:
					for j in range(0,len(test1)):
						if re.search(i,test1[j]) != None:
							test3 = re.split(i,test1[j])
							for k in cname_key3:
								if re.search(k,test3[0]) != None:
									test2 = re.split(k,test3[0])
									test4 = re.split(cname_key2,test3[1])
									if test4[0] == '':
										continue
									else:
										if test2[-1] in charge_name and test4[0] in charge_name:
											break
										else:
											if test2[-1] in charge_name and test4[0] not in charge_name:
												charge_name[charge_name.index(test2[-1])+1] = test4[0]
												break
											else:
												charge_name.append(test2[-1])
												charge_name.append(test4[0]) 
							
									break'''

			if len(charge_name)>=2:
				num=int(len(charge_name)/2)
				for i in range(num):
					charge_name_txt[charge_name[2*i]]=charge_name[2*i+1]
			else:
				charge_name_txt={"未抽取成功":"未抽取成功"}
			return charge_name_txt

'''案件类型抽取,传参为罪名抽取'''
def cname2(charge_name_txt):
	doc_name=[]
	if(charge_name_txt!=None):
		for key,value in charge_name_txt.items():
			if(value not in doc_name):
				doc_name.append(value)
	return doc_name

'''抽判决书编号'''
def Decision_number(str):
    unit = ""
    text3 = []
    text = str.split('。')
    for i in range(len(text)):
        if  re.search(character[0], text[i]) != None or re.search('\(',text[i])!=None and re.search(character[1],text[i]) != None or re.search('\)',text[i])!=None and re.search(character[2],text[i]) != None and re.search(character[3], text[i]) != None:
            text3 = re.findall(r'[（(].+[）)].+刑.+号', text[i])
            if text3:
                unit = text3[0]
                break
    return unit

'''抽相关判决书编号'''
def other_decision_num(str,decision_num):
    text = str.split("。")
    other_decision_txt=[]
    for i in range(len(text)):
        if i>2and re.search(character[0], text[i]) != None and re.search(character[1],text[i]) != None and re.search(character[2], text[i]) != None and re.search(character[3], text[i]) != None:
            text2=text[i]
            text3=re.findall(r'（\d{4}）.*刑.*号',text2)
            for a in text3:
                if(len(a)<35 and a not in other_decision_txt ):
                    other_decision_txt.append(a)
    return other_decision_txt

'''提取编号与审判级别'''
def level(str):
	unit = ""
	case_hearing_level = ""
	sentence = str.split('。')
	for j in range(0,len(sentence)):
		for n in Des_num_words:
				if n in sentence[j]:
					num = 1
				else:
					num = 0
					break
		if num ==1:
			start = sentence[j].find(Des_num_words[0])
			end = sentence[j].find(Des_num_words[-1])
			unit = sentence[j][start:end+1]
			for key in level_dic:
				if(re.search(key, unit)!= None):
					case_hearing_level = level_dic[key]
			return case_hearing_level


'''公安机关对判决人的处理'''
def process(str,person):
	infor={}
	infor_per = ""
	sentence = str.split("。")
	for each_per in person:
		for i in sentence:
			if re.search(each_per,i):
				for j in per_words:
					if re.search(j,i):
						start = i.find(j)
						infor_per = i[start:]
						infor[each_per]=infor_per
	return infor

'''公安机关对物品的处理'''
def process_(str):
	other_infor = []
	judge_infor = []
	sentence = str.split("。")
	for i in range(0,len(sentence)):
		if not judge_words in sentence[i]:
			for m in other_detain_words[0]:
				if m in sentence[i]:
					num = 0
					break
				else:
					num = 1
			if num == 1:
				for p in other_detain_words[-1]:
					sentence_ = sentence[i].replace('\n', '')
					if p in sentence_ and not sentence_ in other_infor:
						start = sentence_.find(judge_words)
						if  start != -1:
							other_infor.append(sentence_[start:])
						else:
							other_infor.append(sentence_)
		else:
			for each_sentence in sentence[i:]:
				for w in judge_detain_words_1:
					each_sentence_ = each_sentence.replace('\n', '')
					if w in each_sentence_ and not each_sentence_ in judge_infor:
						judge_infor.append(each_sentence_)
			break

	return other_infor , judge_infor

'''抽取判决单位的方法定义'''
def Judgment(str):
	judgment_txt=""
	result=re.search(pattern1, str)
	if(result!=None):
		result1=result.group().split(split_pattern)[0]
		judgment_txt=result1+split_pattern
		return judgment_txt

'''抽取法院等级的方法定义'''
def courtlevel(str):
	if(str==None):
		return ""
	courtlevel_txt=""
	result=re.search(pattern2,str)
	if(result!=None):
		courtlevel_txt=result.group(0).strip()
		return courtlevel_txt
	else:
		return level

'''抽取判决单位地点的方法定义'''
def courtpos(str):
	if(str==None):
		return ""	
	pos_txt=""
	result=re.search(pattern2,str)
	if(result!=None):
		pos_txt=str[:-6]
	else:
		pos_txt=str[:-4]
	return pos_txt

'''人身权利抽取方法定义'''
def right(str):
	content=str.split("。")
	right=[]
	right_txt={}
	for i in range(len(content)):
		list1=None
		if(re.search((pattern3),content[i])!=None):
			list1=re.search((pattern3),content[i]).group(0)
		if(re.search((pattern4),content[i])!=None):
			list1=re.search((pattern4),content[i]).group(0)
		if(list1!=None):
			mid_person=re.search(pattern5,content[i])
			if(mid_person!=None):
				person=mid_person.group()[3:-1]
				right.append(person)
				right.append(list1)
	num=int(len(right)/2)
	for i in range(num):
		right_txt[right[2*i]]=right[2*i+1]	
	return right_txt

'''判决生效时间判决方法'''
def publish(str):
	content=str.split("。")
	publish=[]
	publish_txt={}
	flag=0
	for i in range(len(content)):
		mid_res=re.search(string,content[i])
		if(mid_res!=None):
			flag=1
		if(flag==1):
			mid_content=content[i].split("，")
			for i in range(len(mid_content)):
				result=re.search(pattern6,mid_content[i])
				if(result!=None):
					publish.append(result.group())
				person=re.search(pattern5,mid_content[i])
				if(person!=None):
					publish.append(person.group()[3:-1])	
	num=int(len(publish)/2)
	for i in range(num):
		publish_txt[publish[2*i]]=publish[2*i+1]					
	return publish_txt

'''判决时间'''	
def time_ex(str):
	time=""
	year_new=""
	month_new=""
	data_new=""
	year=""
	month=""
	data=""
	new_time=""
	txtlist = str.split("。")
	for a in range(len(txtlist)):
		for flag in time_suffix:
			if re.search(flag, txtlist[a]):
				time = txtlist[a]
	if(len(time)>0):
		txt=time.split("年")
		year=txt[0]
		year=year
		if(len(txt)>1):
			txt2=txt[1].split("月")
			month=txt2[0]
			if(len(txt2)>1):
				txt3=txt2[1].split("日")
				data=txt3[0]
	for a in year:
		if(digital_conversion.get(a)!=None):
			c=digital_conversion[a]
			year_new+=c
	year_new=year_new[0:4]
	if(digital_conversion2.get(month)!=None):
		month_new=digital_conversion2[month]
	if(digital_conversion3.get(data)!=None):
		data_new=digital_conversion3[data]
	if(year_new!="" and month_new!="" and data_new!=""):
		new_time=year_new+"-"+month_new+"-"+data_new
	return new_time

'''案件名称'''	
def casename(f):
	a = f.split('.')[0]
	b=a.split('/')
	casename=b[len(b)-1]
	return casename

'''人物信息抽取'''
def people_infor_ex(text,person):
	oldmoney_txt,fine_txt,moshou_all_txt,moshou_some_txt,tuipei_txt,zangkuan_txt,own_txt,other_txt=fine(text,person)#提取金额
	charge_name_txt=cname(text,person)#罪名抽取
	publish_txt=publish(text)#生效宣告刑刑期
	right_txt=right(text)#人身权利抽取

	detail_people=[]
	detail_people_name=[]
	content=text.split("。")
	for txt in content:
		for per_name in person:
			if re.search(per_name,txt):
				the_people=people()
				the_people.name=per_name
				if re.search(man_sex, txt):#性别
					the_people.sex=man_sex
				if re.search(woman_sex, txt):#性别
					the_people.sex=woman_sex
				for the_nation in nation:	#民族
					if re.search(the_nation, txt):
						the_people.nation=the_nation
				for the_pre_name in pre_name:	#曾用名	
					if re.search(the_pre_name, txt):
						txt1=txt.split(the_pre_name)
						if(len(txt1)>1):
							txt2=txt1[1]
							txt3=txt2.split('，')
							the_people.pre_name=txt3[0]
				for the_nick_name in nick_name:	#绰号
					if re.search(the_nick_name, txt):
						txt1=txt.split(the_nick_name)
						if(len(txt1)>1):
							txt2=txt1[1]
							txt3=txt2.split('，')
							the_people.nick_name=txt3[0]
				if re.search(idcard, txt):#证件号码,证件类型
					txt1=txt.split(idcard)
					if(len(txt1)>1):
						txt2=txt1[1]
						txt3=txt2.split('，')
						the_people.idcard=txt3[0]
						the_people.type_idcard='居民身份证'
				if re.search(birth, txt):#出生日期
					txt1=txt.split(birth)
					txt2=txt1[0]
					txt3=txt2.split('，')
					txt4=txt3[len(txt3)-1]
					the_people.date_birth=txt4+birth_tail
				for the_address in address:	#户籍地
					if re.search(the_address, txt):
						txt1=txt.split(the_address)
						if(len(txt1)>1):
							txt2=txt1[1]
							txt3=txt2.split('，')
							the_people.add_domicile=txt3[0]
				for the_add_live in add_live:	#住所地
					if re.search(the_add_live, txt):
						txt1=txt.split(the_add_live)
						if(len(txt1)>1):
							txt2=txt1[1]
							if(len(txt2)<25):
								the_people.add_live=txt2
								the_people.add_live_detailed=txt2
				for the_edu in edu:	#受教育情况
					if re.search(the_edu, txt):
						txt1=txt.split(the_edu)
						txt2=txt1[0]
						txt3=txt2.split('，')
						txt4=txt3[len(txt3)-1]
						the_people.background_edu=txt4+edu_tail	
				if 	the_people.name not in 	detail_people_name:
					detail_people_name.append(the_people.name)
					detail_people.append(the_people)
	for per in 	detail_people:
		#提取金额
		for key in oldmoney_txt:
			if(per.name==key):
				per.oldmoney_txt=oldmoney_txt[key]		
		for key in fine_txt:
			if(per.name==key):
				per.fine_txt=str(fine_txt[key])
		for key in moshou_all_txt:
			if(per.name==key):
				per.moshou_all_txt=str(moshou_all_txt[key])
		for key in moshou_some_txt:
			if(per.name==key):
				per.moshou_some_txt=str(moshou_some_txt[key])
		for key in tuipei_txt:
			if(per.name==key):
				per.tuipei_txt=str(tuipei_txt[key])
		for key in zangkuan_txt:
			if(per.name==key):
				per.zangkuan_txt=str(zangkuan_txt[key])
		for key in own_txt:
			if(per.name==key):
				per.own_txt=str(own_txt[key])
		for key in other_txt:
			if(per.name==key):
				per.other_txt=str(other_txt[key])
		#罪名
		if(charge_name_txt!=None):
			for key in charge_name_txt:
				if(per.name==key):
					per.summary_SXPJ=charge_name_txt[key]
		#生效宣告刑刑期
		for key in publish_txt:
			if(per.name==key):
				per.period_punishment_SX=publish_txt[key]
		#剥夺政治权利期限
		for key in right_txt:
			if(per.name==key):
				per.right_txt=right_txt[key]																																			
	return detail_people

'''txt文本抽取'''
def extract(path):
	f1 = open("result.txt","a") 
	f1.write("\n\n")
	if os.path.isfile(path):
		#print("文件:"+path)
		a=open(path,"r")
		print("文件:"+path)
		text=""
		while True:
			b=a.readline()
			if(b==""):
				break
			text+=b
		
		#oldmoney_txt,fine_txt,moshou_all_txt,moshou_some_txt,tuipei_txt,zangkuan_txt,own_txt,other_txt=fine(text,person)#提取金额
		#charge_name_txt=cname(text,person)#罪名抽取
		#doc_name=cname2(charge_name_txt)#案件类型抽取
		#crime_txt=crimes_determine(text)#罪行判定
		#infor_person = process(text, person)#公安机关对人的处理
		#right_txt=right(text)#人身权利抽取
		#publish_txt=publish(text)#判决生效时间
		person,person_num=person_extract(text)#涉案人及涉案人数		
		identifier=Decision_number(text)#生效判决文书文号
		other_decision_txt=other_decision_num(text,Decision_number(text))#抽相关判决书编号
		case_hearing_level_txt = level(text)#审判级别		
		other_infor , judge_infor = process_(text)#其他扣押处理,公安机关扣押物品情况
		name=Judgment(text)#生效判决法院
		courtlevel_txt=courtlevel(name)#法院等级
		pos_txt=courtpos(name)#法院地区
		casename_txt=casename(path)#案件名称
		date = time_ex(text)#生效判决日期

		detail_people=people_infor_ex(text,person)#人物信息

		
		'''信息抽取类'''
		my_extract_infor=extract_infor()
		my_extract_infor.identifier=identifier#抽判决书编号
		my_extract_infor.other_decision_txt=other_decision_txt#抽相关判决书编号
		my_extract_infor.case_hearing_level_txt=case_hearing_level_txt#审判级别
		my_extract_infor.judge_infor=judge_infor#公安机关对人的处理
		my_extract_infor.other_infor=other_infor#其他扣押处理
		my_extract_infor.name=name#判决单位
		my_extract_infor.courtlevel_txt=courtlevel_txt#法院等级
		my_extract_infor.pos_txt=pos_txt#法院地区
		my_extract_infor.casename_txt=casename_txt#案件名称
		my_extract_infor.date=date#生效判决日期

		my_extract_infor.detail_people=detail_people#人物信息	
		#my_extract_infor.person=person
		#my_extract_infor.person_num=person_num#涉案人数
		#my_extract_infor.oldmoney_txt=oldmoney_txt#原文本金额
		#my_extract_infor.fine_txt=fine_txt#罚金
		#my_extract_infor.moshou_all_txt=moshou_all_txt#没收全部财产
		#my_extract_infor.moshou_some_txt=moshou_some_txt#没收部分财产
		#my_extract_infor.tuipei_txt=tuipei_txt#责令退赔
		#my_extract_infor.zangkuan_txt=zangkuan_txt#赃款赃物
		#my_extract_infor.own_txt=own_txt#供犯人所用本人财务
		#my_extract_infor.other_txt=other_txt#其他
		#my_extract_infor.doc_name=doc_name#案件类型抽取
		#my_extract_infor.charge_name_txt=charge_name_txt#罪名抽取
		#my_extract_infor.crime_txt=crime_txt#罪行判定
		#my_extract_infor.right_txt=right_txt#人身权利抽取
		#my_extract_infor.publish_txt=publish_txt#判决生效时间					
		
		my_extract=my_extract_infor.__dict__ #所有的信息封装成一个字典
		#print(my_extract)
		#所有的信息封装成一个json,写入result.txt，用于后续抽检
		js = json.dumps(my_extract_infor, default=lambda my_extract_infor: my_extract_infor.__dict__, sort_keys=True, indent=4, ensure_ascii=False)
		f1.write(js)
		
		
	a.close()
	
'''The main function'''
if __name__ == '__main__':
	f1 = open("result.txt","w") 
	f1.truncate()
	f1.close()
	json_path='/home/wangchenwei/juanzong/20181127/jsontotxt'#txt数据
	list = os.listdir(json_path) #列出文件夹下所有的目录与文件
	for i in range(0,len(list)):
		path = os.path.join(json_path,list[i])#path即路径
		if os.path.isfile(path):
			extract(path)
	print ("success")
